package com.validateCodeYzm.zpf.lib;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils;


import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author yeqin
 * @description
 * @date 16:33 2020/10/10
 */
public class JsonUtil {

    public  static <T> Result<T> getObjectMapper(LamUtil.MapperFunction<ObjectMapper,T> fn){
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            T apply = fn.apply(mapper);
            return Result.of(apply);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("json convert error:"+e.getMessage(),e);
        }
    }

    public static <T> Result<T> readMap(Map<String,String> map, Class<T>clazz){
        return  getObjectMapper(objectMapper -> {
            String str = objectMapper.writeValueAsString(map);
            return objectMapper.readValue(str,clazz);
        });
    }

    public  static <T> Result<T> readMap(String jsonStr, Class<T> clazz){
        if(StringUtils.isEmpty(jsonStr))
            return Result.error("data is empty");
        return getObjectMapper(objectMapper -> objectMapper.readValue(jsonStr, clazz));
    }

    public  static <T> Result<T> readMap(String jsonStr, TypeReference<T> reference){
        if(StringUtils.isEmpty(jsonStr))
            return Result.error("data is empty");
        return getObjectMapper(objectMapper -> objectMapper.readValue(jsonStr, reference));
    }

    public static Result<String> writeString(Object object){
        return getObjectMapper(objectMapper -> objectMapper.writeValueAsString(object));
    }

    public static Result<Map<String,String>> StringToMapper(String value){
        return readMap(value, new TypeReference<Map<String, String>>() {
        });
    }

    public static String writeSimpleMap(Map<String, Object> maps) {

        StringBuffer stringBuffer = new StringBuffer("{");

        if(Objects.nonNull(maps)){
            maps.forEach((k,v)->{
                stringBuffer.append("\""+k+"\":");

                if(v.getClass().equals(String.class)){
                    stringBuffer.append("\""+((String) v).replaceAll("\"","\\\\\"")+"\"");
                }else{
                    stringBuffer.append(v);
                }

                stringBuffer.append(",");
            });
        }

        stringBuffer.deleteCharAt(stringBuffer.length()-1);

        stringBuffer.append("}");

        return stringBuffer.toString();

    }

    public static Map<String,String> getSimpleMap(Object object){
        try{
            Map<String,String> param = new HashMap<>();

            Field[] declaredFields = object.getClass().getDeclaredFields();

            for(Field field:declaredFields){

                Object o = field.get(object);

                if(Objects.nonNull(o)){
                    param.put(field.getName(),String.valueOf(o));
                }
            }
            return param;
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException("序列化失败：getSimpleMap："+object.getClass().getName());
        }
    }

}
